package cn.fsd.service.impl;

import cn.fsd.domain.Courier;
import cn.fsd.mapper.CourierMapper;
import cn.fsd.mapper.TaskMapper;
import cn.fsd.query.substation.CourierQuery;
import cn.fsd.service.CourierService;
import cn.fsd.vo.CourierBean;

import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CourierServiceImpl implements CourierService {

  @Autowired
  CourierMapper courierMapper;
  @Autowired
  TaskMapper taskMapper;

  @Override
  public List<Courier> getAvailableCouriers(CourierQuery query) {
    List<Courier> res = courierMapper.getAvailableCourierByStationId(query);
    res = res.parallelStream().map(item -> {
      CourierBean bean = new CourierBean(item);
      bean.setTask_num(taskMapper.getCourierTasks(query.getStation_id(), item.getCourier_id()));
      return bean;
    }).collect(Collectors.toList());
    res.sort((item1, item2) -> {
      CourierBean bean1 = (CourierBean) item1;
      CourierBean bean2 = (CourierBean) item2;
      return bean1.getTask_num().compareTo(bean2.getTask_num());
    });
    return res;
  }
}
